@echo off
chcp 65001 >nul

:: 从build.ini读取项目目录
for /f "usebackq tokens=*" %%I in (`type build.ini`) do set SOLUTION_DIR=%%I

rem 验证是否找到了目录
if not defined SOLUTION_DIR (
    echo Error: Solution directory not found in build.ini.
    exit /b 1
)
echo SOLUTION_DIR: %SOLUTION_DIR%

rem 初始化变量
set "PROJECT_NAME=LE_KgPlus"
set "VERSION="
set "CONFIGURATION=Release"
set "PROJECT_TO_BUILD=%PROJECT_NAME%"
rem 默认用户版本设为normal
set "USER_VERSION=normal"
set "BUILD_ALL=false"
set "BUILD_Other_ALL=false"

:loopArgs
if "%1"=="" goto :endArgs
if /i "%1"=="-v" (
    if "%2"=="" (
        echo Error: Version number must follow '-v'.
        exit /b 1
    )
    set "VERSION=%2"
    shift
)
if /i "%1"=="-u" (
    if "%2"=="" (
        echo Error: User version must follow '-u'.
        exit /b 1
    )
    set "USER_VERSION=%2"
    call :setConfig
    shift
)
if /i "%1"=="-p" (
    if /i "%2"=="ntp" (
        set "PROJECT_TO_BUILD=NtpServer_ForMods"
    ) else if /i "%2"=="ssl" (
        set "PROJECT_TO_BUILD=SSLServer_ForMods"
    ) else if /i "%2"=="update" (
        set "PROJECT_TO_BUILD=UpdateServer_ForMods"
    ) else if /i "%2"=="all" (
        set "BUILD_Other_ALL=true"
    ) else (
        echo Error: Invalid project name '%2'. Valid options are 'ntp', 'ssl', 'update', or 'all'.
        exit /b 1
    )
    echo PROJECT_TO_BUILD: %PROJECT_TO_BUILD%
    shift
)
if /i "%1"=="-a" (
    set "BUILD_ALL=true"
)
shift
goto :loopArgs
:endArgs

rem 验证版本号是否已设置
if "%VERSION%"=="" (
    echo Error: Version number is required.
    exit /b 1
)

echo BUILD_ALL: %BUILD_ALL%
echo BUILD_Other_ALL: %BUILD_Other_ALL%

rem 设置编译配置
if /i "%BUILD_ALL%"=="true" (
    call :buildAll
) else if /i "%BUILD_Other_ALL%"=="true" (
    call :buildOtherAll
) else (
    call :buildSingle
)
exit /b 0


:buildAll
echo -----批量构建 [构建所有用户版本]-----
for %%u in (normal cheat advance) do (
    call :buildProject %%u
)
goto :eof

:buildOtherAll
echo -----批量构建 [构建所有其他项目]-----
for %%O in (NtpServer_ForMods SSLServer_ForMods UpdateServer_ForMods) do (
    call :buildSingleProject %%O
)
goto :eof

:buildSingle
call :buildProject %USER_VERSION%
goto :eof

:buildProject
setlocal
set "USER_VERSION=%~1"
call :setConfig
echo User Version: %USER_VERSION%
echo Version: %VERSION%
echo Building project: %PROJECT_TO_BUILD%
if /i "%PROJECT_TO_BUILD%"=="LE_KgPlus" (
    echo Configuration: %CONFIGURATION%
) else (
    echo Configuration: Release
    set "CONFIGURATION=Release"
)

echo cd to %SOLUTION_DIR%\%PROJECT_TO_BUILD%
pushd "%SOLUTION_DIR%\%PROJECT_TO_BUILD%"

:: 执行构建命令
echo dotnet build -c "%CONFIGURATION%"
dotnet build -c "%CONFIGURATION%"

:: 检查构建是否成功
if %ERRORLEVEL% neq 0 (
    echo Error: Build failed for %PROJECT_TO_BUILD% with configuration %CONFIGURATION%
    exit /b 1
)
popd

:: 拷贝生成文件
if /i "%PROJECT_TO_BUILD%"=="LE_KgPlus" (
    call :copyFiles
)


goto :eof

:buildSingleProject
setlocal
set "PROJECT_TO_BUILD=%~1"
echo Building project: %PROJECT_TO_BUILD%
echo Configuration: Release
set "CONFIGURATION=Release"
echo cd to %SOLUTION_DIR%\%PROJECT_TO_BUILD%
pushd "%SOLUTION_DIR%\%PROJECT_TO_BUILD%"

:: 执行构建命令
echo dotnet build -c "%CONFIGURATION%"
dotnet build -c "%CONFIGURATION%"

:: 检查构建是否成功
if %ERRORLEVEL% neq 0 (
    echo Error: Build failed for %PROJECT_TO_BUILD% with configuration %CONFIGURATION%
    exit /b 1
)
popd

goto :eof

:copyFiles
set "DLL_PATH=%SOLUTION_DIR%\%PROJECT_TO_BUILD%\bin\%CONFIGURATION%\net6.0\Obfuscar\%PROJECT_TO_BUILD%.dll"
set "TARGET_DIR=%cd%\%VERSION%\%USER_VERSION%\Mods"
if not exist "%TARGET_DIR%" (
    echo Creating target directory: %TARGET_DIR%
    mkdir "%TARGET_DIR%"
)
echo Copying %DLL_PATH% to %TARGET_DIR%
copy "%DLL_PATH%" "%TARGET_DIR%" >nul
if %ERRORLEVEL% neq 0 (
    echo Error: Failed to copy %DLL_PATH% to %TARGET_DIR%
    exit /b 1
)
goto :eof

:setConfig
if /i "%USER_VERSION%"=="normal" (
    set "CONFIGURATION=Release"
) else if /i "%USER_VERSION%"=="cheat" (
    set "CONFIGURATION=CheatVersion"
) else if /i "%USER_VERSION%"=="advance" (
    set "CONFIGURATION=Advance"
)
goto :eof